package net.zigin.nhi.could.waste.service.impl;

import net.zigin.commons.util.AssertUtil;
import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.commons.util.StringUtil;
import net.zigin.nhi.could.waste.aop.LoginAspect;
import net.zigin.nhi.could.waste.vo.TokenInfoBo;
import net.zigin.nhi.could.waste.dto.SysMenuDto;
import net.zigin.nhi.could.waste.service.SysUserService;
import net.zigin.nhi.could.waste.vo.SysMenuVo;
import net.zigin.nhi.could.waste.vo.SysUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.zigin.nhi.could.waste.dao.BaseMapper;
import net.zigin.nhi.could.waste.dao.SysMenuMapper;
import net.zigin.nhi.could.waste.model.SysMenu;
import net.zigin.nhi.could.waste.service.SysMenuService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*#################################################################*
# 说明:以下代码通过哲勤代码生成工具自动生成    
# 工具:GeneratorCode
# 作者:mgw1986                                                  
# 生成时间:2021-9-7 19:08:53                                               
##################################################################*/


/**
 * SysMenu服务实现类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: SysMenuServiceImpl
 * @date 2021-9-7 19:08:53
 */
@Service
public class SysMenuServiceImpl extends AbstractServiceImpl<SysMenu, String> implements SysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysUserService sysUserService;

    @Override
    public BaseMapper<SysMenu, String> getMapper() {
        return sysMenuMapper;
    }

    @Override
    public String saveOrUpdate(SysMenuDto menuDto) {
        SysUserVo currentUser = sysUserService.getCurrentUser();
        if (StringUtil.isNullOrEmpty(menuDto.getSysFlag())) {
            menuDto.setSysFlag(currentUser.getType());
        }
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        if (StringUtil.isNullOrEmpty(menuDto.getId())) {
            AssertUtil.isNull(menuDto, "菜单信息不能为空");
            AssertUtil.isNull(menuDto.getName(), "菜单名称不能为空");
            AssertUtil.isNull(menuDto.getUrl(), "菜单路由不能为空");
            AssertUtil.isNull(menuDto.getSysFlag(), "系统标识不能为空");
            AssertUtil.isNull(menuDto.getType(), "菜单类型不能为空");
            // 新增
            menuDto.setId(StringUtil.get32UUID());
            menuDto.setCreateTime(new Date());
            menuDto.setModifyTime(new Date());
            menuDto.setCreateUser(tokenInfo.getLoginName());
            SysMenu menu = ReflectUtil.copyProperties(menuDto, SysMenu.class);
            sysMenuMapper.insert(menu);
            return menu.getId();
        }
        // 编辑
        menuDto.setModifyTime(new Date());
        menuDto.setModifyUser(tokenInfo.getLoginName());
        SysMenu menu = ReflectUtil.copyProperties(menuDto, SysMenu.class);
        sysMenuMapper.updateByPrimaryKeySelective(menu);
        return menu.getId();
    }


    @Override
    public List<SysMenuVo> getMenuList(SysMenuDto menuDto) {
        // todo 只返回
        Map<String, Object> paramMap = getMenuParams(menuDto);
        List<SysMenu> menus = new ArrayList<>();
        if (menuDto.getIsPage()) {
            menus = sysMenuMapper.selectListByPage(paramMap);
        } else {
            menus = sysMenuMapper.selectListByCondition(paramMap);
        }
        List<SysMenuVo> menuVos = ReflectUtil.copyProperties(menus, SysMenuVo.class);
        if (menuVos != null) {
            menuVos.stream().filter(sysMenuVo -> !StringUtil.isNullOrEmpty(sysMenuVo.getParentId())).forEach(sysMenuVo -> {
                SysMenu sysMenu = sysMenuMapper.selectByPrimaryKey(sysMenuVo.getParentId());
                sysMenuVo.setParentName(sysMenu.getName());
            });
            return menuVos;
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public Integer getMenuCount(SysMenuDto menuDto) {
        Map<String, Object> paramMap = getMenuParams(menuDto);
        return sysMenuMapper.selectCount(paramMap);
    }

    private Map<String, Object> getMenuParams(SysMenuDto menuDto) {
        if (StringUtil.isNullOrEmpty(menuDto.getSysFlag())) {
            SysUserVo currentUser = sysUserService.getCurrentUser();
            menuDto.setSysFlag(currentUser.getType());
        }
        return ParamsMapUtil.getInstance()
                .addParam("sysFlag", menuDto.getSysFlag())
                .addParam("name", menuDto.getName())
                .addParam("parentId", menuDto.getParentId())
                .addParam("url", menuDto.getUrl())
                .addParam("pageIndex", menuDto.getPageIndex())
                .addParam("pageSize", menuDto.getPageSize());
    }

    @Override
    public List<SysMenuVo> infoByRoleId(String roleId) {
        return sysMenuMapper.infoByRoleId(roleId);
    }

    @Override
    public List<SysMenuVo> infoByUserId(String userId) {
        return sysMenuMapper.infoByUserId(userId);
    }

    @Override
    public List<SysMenuVo> getHospitalMenuList() {
        return sysMenuMapper.infoBySysFlag("hospital");
    }

    @Override
    public List<SysMenuVo> getAdminMenuList() {
        return sysMenuMapper.infoBySysFlag("admin");
    }

    @Override
    public Boolean judgeRemove(String id) {
        // todo 判断有无下级类目
        Integer children = sysMenuMapper.judgeExistChildren(id);
        Integer count = sysMenuMapper.judgeRemove(id);
        return (count + children) > 0;
    }
}